package edu.jd.mainframe;

import edu.jd.aflib.AfColumnLayout;
import edu.jd.aflib.AfXLayout;
import edu.jd.homeworkteacher.CorrectHomeworkDto;
import edu.jd.homeworkteacher.HomeworkTeaService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class CorrectDialog extends JDialog {
    JLabel hkTitleLab = new JLabel("作业标题：");
    JLabel hkTitle = new JLabel();
    JLabel hkSubmitLab = new JLabel("学生提交：");
    JLabel hkSubmit = new JLabel();
    JLabel feedbackLab = new JLabel("作业点评：");
    JTextArea feedbackTA = new JTextArea(5, 20);
    JLabel scoreLab = new JLabel("评分：");
    JTextField scoreTF = new JTextField(5);
    JButton ok = new JButton("确定");
    private boolean isOK = false;
    private List<CorrectHomeworkDto> correctHomeworkDtoList;
    private int index;

    private HomeworkTeaService homeworkTeaService = new HomeworkTeaService();

    public CorrectDialog(){}

    public CorrectDialog(JFrame owner, List<CorrectHomeworkDto> list, int index){
        super(owner, "批改作业", true);
        correctHomeworkDtoList = list;
        this.index = index;
        if(index<correctHomeworkDtoList.size()){
            showing();
        }else{
            JOptionPane.showMessageDialog(owner, "暂时没有未批改的作业！");
        }
    }

    private void showing() {
        this.setSize(500, 400);
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());
        JPanel main = new JPanel();
        main.setLayout(new AfColumnLayout());
        root.add(main, BorderLayout.CENTER);

        CorrectHomeworkDto dto = correctHomeworkDtoList.get(index);

        JPanel titleP = new JPanel();
        titleP.setLayout(new AfXLayout());
        titleP.add(hkTitleLab);
        hkTitle.setText(dto.getHk_title());
        titleP.add(hkTitle);
        main.add(titleP);

        main.add(hkSubmitLab);
        hkSubmit.setText(dto.getHk_submit());
        main.add(hkSubmit);

        JPanel scoreP = new JPanel();
        scoreP.setLayout(new AfXLayout());
        main.add(scoreP);
        scoreP.add(scoreLab);
        scoreP.add(scoreTF);
        main.add(feedbackLab);
        main.add(feedbackTA);

        root.add(ok, BorderLayout.PAGE_END);

        ok.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent a){
                if(feedbackTA.getText().length()>0 && scoreTF.getText().length()>0){
                    homeworkTeaService.correctHomework(dto.getHk_id(), dto.getStu_id(),
                            Integer.parseInt(scoreTF.getText()), feedbackTA.getText());
                    CorrectDialog.this.setVisible(false);
                    JOptionPane.showMessageDialog(root, "提交成功！");
                    isOK = true;
                }else{
                    JOptionPane.showMessageDialog(root, "作业点评或评分不能为空！");
                }
            }
        });
        this.setVisible(true);
    }

    public boolean isOk(){
        return isOK;
    }
}
